package org.example.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.pojo.CondiRo;
import org.example.pojo.EmployeeAndName;
import org.example.pojo.EmployeeScoreVo;
import org.example.pojo.Score;
import org.example.service.PerformanceScoreService;
import org.example.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 绩效评分
 *
 * @author Administrator
 * @date 2022/09/03
 */
@Api(tags = "绩效评分")
@RestController
@RequestMapping("/PerformanceScore")
public class PerformanceScoreController {
    @Autowired
    private PerformanceScoreService performanceScoreService;

    /**
     * 分页员工绩效评分信息
     *
     * @return {@link ResponseData}
     */
    @GetMapping("/ShowEmployeeScore/{page}/{limit}")
    @ResponseBody
    @ApiOperation("分页员工绩效评分")
    public ResponseData EmployeePerformanceScore(@ApiParam("当前页") @PathVariable int page,
                                                 @ApiParam("每一页显示几条数据") @PathVariable int limit){
        //开启分页功能
        PageHelper.startPage(page, limit);
        List<EmployeeScoreVo> employeeScoreVos = performanceScoreService.selectEmployeeScore();
        //PageInfo对象
        PageInfo<EmployeeScoreVo> employeeScoreVoPageInfo = new PageInfo<>(employeeScoreVos);

        ResponseData responseData = ResponseData.getJsonData(employeeScoreVos, (int) employeeScoreVoPageInfo.getTotal());
        return responseData;
    }

    /**
     * id查询员工详细信息
     *
     * @param id id
     * @return {@link ResponseData}
     */
    @GetMapping("/EmployeeDetails/{id}")
    @ResponseBody
    @ApiOperation("id查询员工详细信息")
    public  ResponseData EmployeeDetails(@ApiParam("员工Id") @PathVariable int id){
        //通过id查询员工详细信息
        EmployeeAndName employeeAndName = performanceScoreService.selectEmployeeById(id);
        ResponseData responseData = new ResponseData();
        responseData.setData(employeeAndName);
        return responseData;
    }

    /**
     * 绩效评分记录
     *
     * @return {@link ResponseData}
     */
    @GetMapping("/ScoreRecord/{page}/{limit}")
    @ResponseBody
    @ApiOperation("绩效评分记录")
    public ResponseData ScoreRecord(
                                    @ApiParam("当前页") @PathVariable int page,
                                    @ApiParam("每页大小") @PathVariable int limit,
                                    @ApiParam("查询关键字") String message){
        if ("undefined".equals(message)){
            message=null;
        }
        System.out.println(message+"---------------");
        //开启分页
        PageHelper.startPage(page,limit);
        List<Score> scores = performanceScoreService.selectScoreRecord(message);
        //根据查询结果生成PageInfo对象
        PageInfo<Score> scorePageInfo = new PageInfo<>(scores);
        ResponseData responseData = ResponseData.getJsonData(scores, (int) scorePageInfo.getTotal());

        return responseData;
    }

    /**
     * 回显员工评分
     *
     * @return {@link ResponseData}
     */
    @GetMapping("/EmployeeScore/{id}")
    @ResponseBody
    @ApiOperation("回显员工评分")
    public ResponseData EmployeeScore(@ApiParam("员工id") @PathVariable int id){
        EmployeeScoreVo employeeScoreVo = performanceScoreService.selectEmployeeScoreById(id);
        ResponseData responseData = new ResponseData();
        responseData.setData(employeeScoreVo);
        return responseData;
    }

    /**
     * 添加分数
     *
     * @return {@link ResponseData}
     */
    @PostMapping("/AddScore/{id}/{grade}")
    @ApiOperation("添加分数")
    public ResponseData AddScore(@ApiParam("员工id") @PathVariable int id,
                                 @ApiParam("分数")@PathVariable Integer grade
                                 ){
        if ("undefined".equals(grade)){
            grade = null;
        }
        int scoreById = performanceScoreService.insertScoreById(id, grade);
        if (scoreById >0){
            return ResponseData.getSccesss("添加成功",100);
        }
        return ResponseData.error("添加失败",101);
    }


    /**
     * 条件查询
     *

     * @return {@link ResponseData}
     */
    @GetMapping("/ConditionQuery")
    @ApiOperation("条件查询")
    @ResponseBody
    public ResponseData ConditionQuery( CondiRo condiRo){
        // 默认分页参数
        if (condiRo.getLimit()==0){
            condiRo.setLimit(10);
        }
        if (condiRo.getPage()==0){
            condiRo.setPage(1);
        }
//        if ("undefined".equals(name)){
//            name = null;
//        }
//        if ("undefined".equals(sex)){
//            sex =null;
//        }
        //开启分页
        PageHelper.startPage(condiRo.getPage(), condiRo.getLimit());
        List<EmployeeScoreVo> employeeScoreVos = performanceScoreService.selectByAndNameOrIdOrSex(condiRo.getName(), condiRo.getId(),condiRo.getSex());
        //根据查询结果生成PageInfo对象
        PageInfo<EmployeeScoreVo> scorePageInfo = new PageInfo<>(employeeScoreVos);
        ResponseData responseData = ResponseData.getJsonData(employeeScoreVos, (int) scorePageInfo.getTotal());
        return responseData;
    }

    /**
     * 更新分数
     *
     * @return {@link ResponseData}
     */
    @PutMapping("/updateScore/{id}/{grade}")
    @ResponseBody
    @ApiOperation("更新分数")
    public ResponseData updateScore(@ApiParam("id")@PathVariable int id,
                                    @ApiParam("分数")@PathVariable Integer grade){

        int update = performanceScoreService.updateById(id, grade);
        if (update > 0){
            return ResponseData.getSccesss("更新成功",100);
        }
        return ResponseData.error("添加失败",101);
    }
}
